import {Component, OnInit} from '@angular/core';
import {AddWmsWarehouseManageComponent} from '../add-wms-warehouse-manage.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {WmsWarehouseManage, WmsWarehouseManageCondition} from '../../wms-warehouse-manage.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import { ActivatedRoute } from '@angular/router';
import { isNullOrUndefined } from 'util';

@Component({
    selector: 'app-main-add-wms-warehouse-manage',
    templateUrl: './main-add-wms-warehouse-manage.component.html',
    styleUrls: ['./main-add-wms-warehouse-manage.component.css']
})
export class MainAddWmsWarehouseManageComponent implements OnInit {

    errRes = new ErrRes();

    domain = new WmsWarehouseManage();

    showHouseAreaLabel = false;

    extend = {
        domain: {
            default: () => {
                this.domain.serAudit = Zero3Enum.AUDIT_FLAG_WAIT_SUBMIT;
                this.domain.serUsing = Zero3Enum.ENABLE;
                this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
            },
            serType: {
                nickname: '库区类型',
                items: [],
                change: (event) => {
                    console.log(event);
                }
            },
            serHouseType: {
                nickname: '仓库类别',
                items: [],
                change: (event) => {
                    console.log(event);
                }
            },
            serArea: {
                nickname: '所在地区',
                items: [],
                change: (event) => {
                    console.log(event);
                }
            },
            serAudit: {
                nickname: ''
            }
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.wmsWarehouseManage.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.wmsWarehouseManage.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        },
        load: {
            wmsHouse: (upper: string) => {
                const data = new WmsWarehouseManageCondition();
                data.uuid = upper;
                Services.wmsWarehouseManage.info(data).subscribe(item => {
                    if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                        this.domain = item.domain;
                        this.domain.uuid = null;
                        this.domain.serName = null;
                        this.domain.serNum = null;
                        this.domain.upper = upper;
                    }
                });
            }
        }
    };

    constructor(public addWmsWarehouseManage: AddWmsWarehouseManageComponent, private active: ActivatedRoute) {
        this.addWmsWarehouseManage.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        Zero3Enum.WMS_HOUSE_AREA_TYPE_FLAG.forEach((value, key) => {
            this.extend.domain.serType.items.push({id: key, name: value});
        });
        Zero3Enum.WMS_HOUSE_AREA_FLAG.forEach((value, key) => {
            this.extend.domain.serArea.items.push({id: key, name: value});
        });
        if (this.active.snapshot.paramMap.has('upper')) {
            this.showHouseAreaLabel = true;
            const upper = this.active.snapshot.paramMap.get('upper');
            this.func.load.wmsHouse(upper);
        }

    }


}
